package com.example.teacher.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.teacher.R;
import com.example.teacher.adpter.MyExpandableAdapter;
import com.example.teacher.base.BaseActivity;
import com.example.teacher.bean.BasicResponse;
import com.example.teacher.bean.StudentBean;
import com.example.teacher.bean.TestCenterListBean;
import com.example.teacher.fragment.CenterTeacherFragment;
import com.example.teacher.fragment.LiftTeacherFragment;
import com.example.teacher.fragment.RigthTeacherFragment;
import com.example.teacher.network.DefaultObserver;
import com.example.teacher.network.NetWorkManager;
import com.google.android.material.tabs.TabLayout;
import com.orhanobut.hawk.Hawk;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
/*
* 老师监考界面，三个机位
* */

public class TeacherInvigilatActivity extends BaseActivity {
    /** 适配器 */
    private MyExpandableAdapter mExpandableAdapter;
    private TextView user_info;
  private TabLayout tabLayout;
    private ExpandableListView expandable_list_view;
    private ViewPager view_pager;
    private String examRoomCode="";
    /** 数据集合 */
    private List<StudentBean.ChildDataBean> childDataBeans=new ArrayList<>();
    private List<StudentBean> studentBeanList=new ArrayList<>();
    private String[] titles = {"机位1", "机位1","机位3"};
    private ViewPager viewPager;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_teacher_invigilate;
    }

    @Override
    protected void initView() {
        expandable_list_view=(ExpandableListView)findViewById(R.id.expandable_list_view);
        viewPager=(ViewPager)findViewById(R.id.view_pager);
        tabLayout=(TabLayout)findViewById(R.id.tabLayout);

    }

    @Override
    protected void initData() {
        if (Hawk.contains("examRoomCode")){
            examRoomCode=Hawk.get("examRoomCode");
        }
      if (examRoomCode.isEmpty()){
          Toast.makeText(TeacherInvigilatActivity.this,"获取列表失败",Toast.LENGTH_SHORT).show();
          return;

      }
        NetWorkManager.getInstance().getNetWorkApiService().getStuList(examRoomCode).compose(this.<BasicResponse<List<StudentBean.ChildDataBean>>>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<BasicResponse<List<StudentBean.ChildDataBean>>>(){
                    @Override
                    protected void onSuccess(BasicResponse<List<StudentBean.ChildDataBean>> response) {

                        if (response != null) {
                            if (response.getData() != null) {
                                childDataBeans=response.getData();
                                for(int i=0;i<childDataBeans.size();i++){
                                    Log.d("dfasdfsaf",childDataBeans.get(i).getName());
                                }
                                StudentBean  studentBean=new StudentBean();
                                studentBean.setGroupName("考生列表");
                                studentBean.setChildData(childDataBeans);
                                studentBeanList.add(studentBean);
                                setData(studentBeanList);
                            }

                        }
                    }

                    @Override
                    protected void onFail(BasicResponse<List<StudentBean.ChildDataBean>> response) {
                        super.onFail(response);
                    }
                });
        initViewPagerFragment();
    }

private void setData(List<StudentBean> studentBeans){
    // 实例化适配器
    mExpandableAdapter = new MyExpandableAdapter(studentBeans, this);
    // 设置适配器
    expandable_list_view.setAdapter(mExpandableAdapter);
    mExpandableAdapter.notifyDataSetChanged();

    // 子item的监听
    expandable_list_view.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            Toast.makeText(TeacherInvigilatActivity.this, childPosition + "", Toast.LENGTH_SHORT).show();
            List<StudentBean.ChildDataBean> childDataBeans = studentBeans.get(groupPosition).getChildData();
            childDataBeans.add(childDataBeans.get(childPosition));
            // 设置数据
            mExpandableAdapter.setExpandableModeList(studentBeans);
            // 通过关闭组item在展开刷新数据
            expandable_list_view.collapseGroup(groupPosition);
            expandable_list_view.expandGroup(groupPosition);
            return false;
        }
    });
    expandable_list_view.expandGroup(0);

}

    private void initViewPagerFragment() {
        if (titles.length < 4) {
            tabLayout.setTabMode(TabLayout.MODE_FIXED);
        } else {
            tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        }//tab的模式如果标签多的话用MODE_SCROLLABLE  少的话用MODE_FIXED
        FragmentManager fm = getSupportFragmentManager();
        FragmentAdapter pagerAdapter = new FragmentAdapter(fm);

        pagerAdapter.addFragment(LiftTeacherFragment.newInstance("机位1"), "机位1");
        pagerAdapter.addFragment(CenterTeacherFragment.newInstance("机位2"), "机位2");
        pagerAdapter.addFragment(RigthTeacherFragment.newInstance("机位3"), "机位3");
        viewPager.setAdapter(pagerAdapter);
        viewPager.setWillNotDraw(true);
        tabLayout.setupWithViewPager(viewPager);

        tabLayout.getBackground().setAlpha(175);

        /**
         * 选择tablayout的监听，一般是用不到的
         */

      tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //不知道干啥的
            }
        });
        tabLayout.getTabAt(1).select(); //默认选中第二个tab
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    class FragmentAdapter extends FragmentPagerAdapter {
        private List<Fragment> mFragments = new ArrayList<>();
        private List<String> mFragmentTitle = new ArrayList<>();

        public FragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);//添加到List中
            mFragmentTitle.add(title);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        //这个方法是用来设置tab的name的
        @Override
        public CharSequence getPageTitle(int position) {
            //从添加后的list中取数据并返回
            return mFragmentTitle.get(position);
        }
    }
}
